package org.jheuristics.ga.operators.reproductors;

import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.operators.Probability;
import org.jheuristics.ga.operators.Reproductor;
import org.jheuristics.util.BitString;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell at ourgrid.org, 2005
 */
public class BitStringGensListMutation extends AbstractGensListMutation {

	/**
	 * TODO
	 *
	 * @param probability
	 * @param tryOuts
	 */
	public BitStringGensListMutation(Probability probability, int tryOuts) {
		super(probability, tryOuts);
	}

	/**
	 * TODO
	 *
	 * @param decoretedReproductor
	 * @param probability
	 * @param tryOuts
	 */
	public BitStringGensListMutation(Reproductor decoretedReproductor, Probability probability, int tryOuts) {
		super(decoretedReproductor, probability, tryOuts);
	}

    /**
     * TODO
     *
     * @param gens
     * @param status
     * @param config
     * @return
     */
    protected Object mutate(Object objectGens, GAStatus status, GAConfig config) {
    	
    	StringBuffer buffer = new StringBuffer();
    	
    	Probability probability = getProbability();
		BitString string = (BitString) objectGens;
		boolean changed = false;
		
		for (int i = 0; i < string.length(); i++) {
			if (probability.check(status, config)) {
				if (!changed) {
					changed = true;
					string = new BitString(string);
				}
				string.flip(i);
				buffer.append("\nMuta��o efetuada no bit: ");
				buffer.append(i+1);
				
			}
		}
		buffer.append("\n");
		logger.debug(buffer);
		
		return string;
    }

}
